package com.toolconclusion.color;

import java.io.PrintStream;

/**
 * 控制台彩色输出工具类
 * 示例：ConsoleColor.green.println("成功信息");
 */
public class ConsoleColor {
    // ANSI 颜色代码
    private static final String RESET = "\033[0m";
    private static final String RED = "\033[31m";
    private static final String GREEN = "\033[32m";
    private static final String YELLOW = "\033[33m";
    private static final String BLUE = "\033[34m";
    private static final String PURPLE = "\033[35m";
    private static final String CYAN = "\033[36m";

    // 彩色输出流（基于 System.out）
    public static final PrintStream red = new ColoredPrintStream(RED);
    public static final PrintStream green = new ColoredPrintStream(GREEN);
    public static final PrintStream yellow = new ColoredPrintStream(YELLOW);
    public static final PrintStream blue = new ColoredPrintStream(BLUE);
    public static final PrintStream purple = new ColoredPrintStream(PURPLE);
    public static final PrintStream cyan = new ColoredPrintStream(CYAN);

    // 自定义彩色 PrintStream
    private static class ColoredPrintStream extends PrintStream {
        private final String colorCode;

        public ColoredPrintStream(String colorCode) {
            super(java.lang.System.out); // 底层使用 System.out
            this.colorCode = colorCode;
        }

        // 重写所有 print/println 方法，添加颜色控制
        @Override
        public void print(String s) {
            super.print(colorCode + s + RESET);
        }

        @Override
        public void println(String s) {
            super.println(colorCode + s + RESET);
        }
    }
}